package com.smart.opcda.topic;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smart.opcda.constant.Constant;
import com.smart.opcda.dto.Group;
import com.smart.opcda.dto.Item;
import com.smart.opcda.dto.Server;
import com.smart.opcda.util.Utils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author dengqiao
 * @date 2024/3/19 16:23
 */
@Component
public class ServerManager {
    private Map<String, Server> serverMap = new ConcurrentHashMap<>();

    private Map<String, Group> groupMap = new ConcurrentHashMap<>();

    private Map<String, Item> itemMap = new ConcurrentHashMap<>();

    @PostConstruct
    public void init() {
        String serverJson = Utils.readFile("server.json");
        JSONArray servers = JSON.parseArray(serverJson);
        for (Object server : servers) {
            JSONObject jsonObject0 = (JSONObject) server;
            String serverId = jsonObject0.getString("serverId");
            String opcServer = jsonObject0.getString("opcServer");
            JSONArray jsonArray = jsonObject0.getJSONArray("group");
            Server server1 = new Server();
            server1.setServerId(serverId);
            server1.setServerName(opcServer);
            if (jsonArray != null && jsonArray.size() > 0) {
                List<Group> groups = new ArrayList<>();
                for (Object object : jsonArray) {
                    Group group = new Group();
                    JSONObject jsonObject = (JSONObject) object;
                    int updateRate = jsonObject.getInteger("updateRate");
                    if (updateRate <= 0) {
                        updateRate = Constant.DEFAULTUPDATERATE;
                    }
                    String groupName = jsonObject.getString("groupName");
                    if (StrUtil.isBlank(groupName)) {
                        groupName = UUID.randomUUID().toString().replaceAll("-", "");
                    }
                    String pubTopic = jsonObject.getString("pubTopic");
                    group.setUpdateRate(updateRate);
                    group.setGroupName(groupName);
                    group.setPubTopic(pubTopic);
                    JSONArray jsonArray1 = jsonObject.getJSONArray("items");
                    if (jsonArray1 != null && jsonArray1.size() > 0) {
                        List<Item> items = new ArrayList<>();
                        for (Object object1 : jsonArray1) {
                            JSONObject jsonObject1 = (JSONObject) object1;
                            String itemName = jsonObject1.getString("itemName");
                            String pubTopic0 = jsonObject1.getString("pubTopic");
                            Item item = new Item();
                            item.setItemName(itemName);
                            item.setPubTopic(pubTopic0);
                            itemMap.put(itemName, item);
                            items.add(item);
                        }
                        group.setItems(items);
                    }
                    groupMap.put(groupName, group);
                    groups.add(group);
                }
                server1.setGroups(groups);
            }
            serverMap.put(serverId, server1);
        }

    }

    public Map<String, Server> getServerMap() {
        return serverMap;
    }

    public Map<String, Group> getGroupMap() {
        return groupMap;
    }

    public Map<String, Item> getItemMap() {
        return itemMap;
    }
}
